#!/usr/bin/env python
#coding=utf-8
import datetime
import utils,db
from ex import NoPreDayData
class Stock:
    def __init__(self, code):
        self.code = code
        self.con = utils.get_db_connenctor()
        
    def previous_day_close(self, y, m, n):
        origin_isodate = utils.get_date_isostr(y,m,n)
        
        for i in [1,3]
            dura = datetime.timedelta(i)
            day = datetime.date(y,m,n) - dura
            isodate = day.isoformat()
            try:
                d = db.lookup_price(self.con, self.code, isodate)
                return d['close']
            except NoPrice e:
                print e
                continue
            
        sql = "select date from price where code=%s order by date"%self.code
        c = self.con.cursor()
        c.execute(sql)
        ds = c.fetchall()
        i = ds.index(origin_isodate)
        if i == 0: raise NoPreDayData(self.code, origin_isodate)
        preday_isostr = ds[i-1]
        d = db.lookup_price(self.con, self.code, preday_isostr)
        return d['close']
    
    def price(self, y, m, n):pass
        
        